package com.dhcc.sdc.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.dto.SdcCdssItemStageDto;
import com.dhcc.sdc.base.service.ISdcStageRelationService;
import com.dhcc.sds.base.cache.qcentity.IQcEntityCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 诊疗阶段关联业务数据
 *
 * @ClassName: SdcStageRelationController
 * @Description TODO
 * @Author caikunshen
 * @Date 2024/6/19
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/sdcstagerelation")
public class SdcStageRelationController {

    private String PREFIX = "/backend/sdc/sdcstagerelation/";

    @Autowired
    private ISdcStageRelationService sdcStageRelationService;

    @RequestMapping("")
    public String index(Model model) { return PREFIX + "sdcstagerelation.html"; }

    /**
     * 获取诊疗阶段关联业务数据的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public JSONArray tree() {
        return sdcStageRelationService.tree();
    }

    /**
     * 获取诊疗方案列表
     */
    @RequestMapping(value = "/getTreatPlanList")
    @ResponseBody
    public Object getTreatPlanlist(@RequestParam Map<String, Object> params) {
        String currentPage = (String) params.get("page");
        String size = (String) params.get("rows");
        Page<SdcCdssItemStageDto> page = new Page<>(Integer.parseInt(currentPage), Integer.parseInt(size));
        List<SdcCdssItemStageDto> result = this.sdcStageRelationService.page(page, params);
        page.setRecords((List<SdcCdssItemStageDto>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增诊疗阶段关联
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @CacheClear(IQcEntityCache.CACHE_NAME)
    public Object add(@RequestParam Map<String, Object> params) {
        if (CommonUtil.isEmpty(params.get("treatPlanId"))) {
            throw new BizException("诊疗方案不能为空");
        }
        sdcStageRelationService.addRelation(params);
        return R.ok();
    }

    /**
     * 删除诊疗阶段关联
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @CacheClear(IQcEntityCache.CACHE_NAME)
    public Object delete(@RequestParam Long stageRelationId) {
        this.sdcStageRelationService.deleteById(stageRelationId);
        return R.ok();
    }

}
